﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;

using Rostfritt.Core;
using Rostfritt.Core.Entities;

namespace Rostfritt.Web.Controllers
{
    public class OrderController : Controller
    {
        //
        // GET: /Order/

        RostfrittContext db = new RostfrittContext();

        public ActionResult Index()
        {
            return View(db.Orders.ToList());
        }

        //
        // GET: /Order/Details/5

        public ActionResult Details(int id)
        {
            var order = db.Orders.Find(id);

            return View(order);
        }

        //
        // GET: /Order/Create

        public ActionResult Create()
        {
            return View(new Order());
        }

        //
        // POST: /Order/Create

        [HttpPost]
        public ActionResult Create(Order order)
        {
            try
            {
                db.Orders.Add(order);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(order);
            }
        }

        //
        // GET: /Order/Edit/5

        public ActionResult Edit(int id)
        {
            var order = db.Orders.Find(id);

            if (order == null)
                return HttpNotFound();

            return View(order);
        }

        //
        // POST: /Order/Edit/5

        [HttpPost]
        public ActionResult Edit(Order order)
        {
            if (ModelState.IsValid)
            {
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(order);
        }

        //
        // GET: /Order/Delete/5

        public ActionResult Delete(int id)
        {
            var order = db.Orders.Find(id);

            if (order == null)
                return HttpNotFound();

            return View(order);
        }

        //
        // POST: /Order/Delete/5

        [HttpPost]
        public ActionResult Delete(Order order)
        {
            try
            {
                db.Orders.Remove(order);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(order);
            }
        }
    }
}
